package instancecreate

import (
	"errors"
	"fmt"
	"gitee.com/laofa1/play/k_conf_mng/kconf_backend/pkg/setting"
	"gitee.com/laofa1/play/k_conf_mng/kconf_backend/pkg/util"
	"log"
)

func DeleteDockerServices(dbServiceNames []string) error {
	var dS string
	for _, eachName := range dbServiceNames {
		inner := fmt.Sprintf(
			"if sudo docker service ls | grep %s; then sudo docker service rm %s; fi;",
			eachName,
			eachName,
		)
		dS = dS + inner
	}

	sshClient, err := util.NewSshClient(setting.DockerManagerHost, setting.DockerManagerPort,
		setting.DockerManagerUser, setting.DockerManagerAuthKey)
	if err != nil {
		log.Println(err.Error())
		return err
	}

	errMsg, err := util.RunSshCommand(sshClient, dS)
	if err != nil {
		log.Println(err.Error())
		return errors.New(errMsg)
	}

	return nil
}